package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Route;
import com.ruoyi.system.domain.ScenicSpot;
import com.ruoyi.system.domain.dto.RouteDetail;
import com.ruoyi.system.domain.dto.RouteDto;
import com.ruoyi.system.mapper.RouteMapper;
import com.ruoyi.system.mapper.ScenicSpotMapper;
import com.ruoyi.system.service.IRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: ZCJ
 * @date: 2024/06/24/11:12
 * @desc:
 */
@Service
public class RouteServiceImpl implements IRouteService {
    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    /**
     * 根据景点id获取路线
     * @param spotId
     * @return
     */
    @Override
    public AjaxResult getRoutesBySpotId(Integer spotId) {
        List<Route> routes = routeMapper.getRoutesBySpotId(spotId);
        return AjaxResult.success(routes);
    }

    /**
     * 根据景点名称获取路线
     * @return
     */
    @Override
    public List<RouteDto> getRoutesBySpotName(String spotName) {
        List<Route> routeList = null;
        if (spotName == null){
            routeList = routeMapper.getAllRoutes();
        }else {
            //查找到当前景点名称的景点id
            List<Integer> spotIds = scenicSpotMapper.getSpotIdsBySpotName(spotName);
            Set<Route> set = new HashSet<>();
            for (Integer spotId : spotIds) {
                List<Route> routes = routeMapper.getRoutesBySpotId(spotId);
                set.addAll(routes);
            }
            routeList = new ArrayList<>(set);
        }
        List<RouteDto> routeDtoList = new ArrayList<>();
        for (Route route : routeList) {
            RouteDto routeDto = new RouteDto();
            routeDto.setRoute(route);
            String[] split = route.getSpotIds().split(",");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < split.length; i++) {
                int id = Integer.parseInt(split[i]);
                ScenicSpot spotById = scenicSpotMapper.getSpotById(id);
                sb.append(spotById.getSpotName());
                if (i != split.length - 1){
                    sb.append(",");
                }
            }
            routeDto.setSpotNames(sb.toString());
            routeDtoList.add(routeDto);
        }
        return routeDtoList;
    }

    @Override
    public AjaxResult getRouteDetailByRouteId(Integer routeId) {
        Route route = routeMapper.getRouteByRouteId(routeId);
        String[] spotIdStr = route.getSpotIds().split(",");
        List<String> spotNames = new ArrayList<>();
        for (String s : spotIdStr) {
            ScenicSpot spot = scenicSpotMapper.getSpotById(Integer.parseInt(s));
            spotNames.add(spot.getSpotName());
        }
        RouteDetail routeDetail = new RouteDetail();
        routeDetail.setRouteId(routeId);
        routeDetail.setDescription(route.getDescription());
        routeDetail.setSpotNames(spotNames);
        return AjaxResult.success(routeDetail);
    }
}
